package cn.jcby.app.lebu.utils;

import cn.jcby.app.filter.dto.LocalUserDTO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author: lihaotian
 * @since: 2023/12/8 01:13
 * @description
 **/
public class SpringSecurityUtils {

    public static <T extends LocalUserDTO> T getCurrentUser() {
        Authentication authentication = getAuthentication();
        if (authentication == null) {
            return null;
        } else {
            Object principal = authentication.getPrincipal();
            return !(principal instanceof LocalUserDTO) ? null : (T) principal;
        }
    }

    public static String getCurrentUserName() {
        Authentication authentication = getAuthentication();
        return authentication != null && authentication.getPrincipal() != null ? authentication.getName() : "";
    }

    public static String getCurrentUserIp() {
        Authentication authentication = getAuthentication();
        if (authentication == null) {
            return "";
        } else {
            Object details = authentication.getDetails();
            if (!(details instanceof WebAuthenticationDetails)) {
                return "";
            } else {
                WebAuthenticationDetails webDetails = (WebAuthenticationDetails)details;
                return webDetails.getRemoteAddress();
            }
        }
    }

    public static boolean hasAnyRole(String... roles) {
        Authentication authentication = getAuthentication();
        if (authentication == null) {
            return false;
        } else {
            Collection<? extends GrantedAuthority> grantedAuthorityList = authentication.getAuthorities();
            String[] var6 = roles;
            int var5 = roles.length;

            for(int var4 = 0; var4 < var5; ++var4) {
                String role = var6[var4];
                Iterator var8 = grantedAuthorityList.iterator();

                while(var8.hasNext()) {
                    GrantedAuthority authority = (GrantedAuthority)var8.next();
                    if (role.equals(authority.getAuthority())) {
                        return true;
                    }
                }
            }

            return false;
        }
    }

    public static void saveUserDetailsToContext(UserDetails userDetails, HttpServletRequest request) {
        PreAuthenticatedAuthenticationToken authentication = new PreAuthenticatedAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
        if (request != null) {
            authentication.setDetails(new WebAuthenticationDetails(request));
        }

        SecurityContextHolder.getContext().setAuthentication(authentication);
    }

    private static Authentication getAuthentication() {
        SecurityContext context = SecurityContextHolder.getContext();
        return context == null ? null : context.getAuthentication();
    }
}
